package com.example.pan.ui.setting

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.pan.aria2.Aria2Repository
import com.example.pan.http.SettingsRepository
import com.example.pan.model.GlobalOption
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import javax.inject.Inject

data class SettingsData(
    val themeMode: Int,
    val globalOption: GlobalOption,
)

@HiltViewModel
class SettingsViewModel @Inject constructor(
    private val aria2Repository: Aria2Repository,
    private val settingsRepository: SettingsRepository
) : ViewModel() {

    @ExperimentalCoroutinesApi
    val settingsData =
        combine(settingsRepository.themeMode, aria2Repository.getGlobalOption()) { f1, f2 ->
            SettingsData(f1, f2)
        }

    fun setThemeMode(themeMode: Int) {
        viewModelScope.launch {
            settingsRepository.setThemeMode(themeMode)
        }
    }
}